/*
 * Copyright Eduardo Aponte 2013
 * aponteeduardo@gmail.com
 */
#include <eyelink.h>
#include "mex.h"
#include "utils.h"



int isscalar (const mxArray *ia){
    if (mxIsNumeric(ia) || mxIsLogical(ia)){
        const int *sia;
        mwSize dia = mxGetNumberOfDimensions(ia);
        sia = mxGetDimensions(ia);
        if ( dia == 2 ){
            if (sia[0] == 1 && sia[1] == 1){
                return 1;
                } else {
                    return 0;
                    }
            } else {
                return 0;
                }
} else {
        return 0;
        }
}


int returnscalar (const mxArray *ia, int *oi ){
    mxClassID id =  mxGetClassID( ia );
    
    if (mxIsComplex(ia)){
        return 0;
    }
    
   if ( id == mxLOGICAL_CLASS ){
        mxLogical  *td;
        td = (mxLogical  *)mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT8_CLASS ){
        signed char *td = (signed char * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT8_CLASS ){
        unsigned char *td = (unsigned char * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT16_CLASS ){
        unsigned short *td = (unsigned short * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT16_CLASS ){
        short *td = (short * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT32_CLASS ){
        unsigned int *td = (unsigned int * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT32_CLASS ){
        int *td = (int * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT64_CLASS ){
        long long *td = (long long * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT64_CLASS ){
        unsigned long long * td = (unsigned long long * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxSINGLE_CLASS ){
        float * td = (float * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxDOUBLE_CLASS ){
            *oi = (int ) *mxGetPr(ia);
            return 1;
    }
    
    return 0;
}

void ievent2struct( IEVENT el_ievent, mxArray **ievent ){
mxArray *t0;
uint32_T *ta0;
mxArray *t1;
int16_T *ta1;
mxArray *t2;
uint16_T *ta2;
mxArray *t3;
int16_T *ta3;
mxArray *t4;
uint32_T *ta4;
mxArray *t5;
uint32_T *ta5;
mxArray *t6;
int16_T *ta6;
mxArray *t7;
int16_T *ta7;
mxArray *t8;
int16_T *ta8;
mxArray *t9;
int16_T *ta9;
mxArray *t10;
uint16_T *ta10;
mxArray *t11;
int16_T *ta11;
mxArray *t12;
int16_T *ta12;
mxArray *t13;
int16_T *ta13;
mxArray *t14;
int16_T *ta14;
mxArray *t15;
uint16_T *ta15;
mxArray *t16;
int16_T *ta16;
mxArray *t17;
int16_T *ta17;
mxArray *t18;
int16_T *ta18;
mxArray *t19;
int16_T *ta19;
mxArray *t20;
uint16_T *ta20;
mxArray *t21;
int16_T *ta21;
mxArray *t22;
int16_T *ta22;
mxArray *t23;
int16_T *ta23;
mxArray *t24;
int16_T *ta24;
mxArray *t25;
int16_T *ta25;
mxArray *t26;
int16_T *ta26;
mxArray *t27;
int16_T *ta27;
mxArray *t28;
int16_T *ta28;
mxArray *t29;
uint16_T *ta29;

const char *fields[] = {"time","type","read","eye","sttime","entime","hstx","hsty",
"gstx","gsty","sta","henx","heny","genx","geny","ena","havx","havy","gavx",
"gavy","ava", "avel","pvel","svel","evel","supd_x","eupd_x","supd_y",
"eupd_y","status"};


*ievent = mxCreateStructMatrix(1,1,30,fields);

t0 = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL);
ta0 = (uint32_T *) mxGetData(t0);
*ta0 = (uint32_T ) el_ievent.time;
mxSetFieldByNumber(*ievent, 0,0,t0);

t1 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta1 = (int16_T *) mxGetData(t1);
*ta1 = (int16_T ) el_ievent.type;
mxSetFieldByNumber(*ievent, 0,1,t1);

t2 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta2 = (uint16_T *) mxGetData(t2);
*ta2 = (uint16_T ) el_ievent.read;
mxSetFieldByNumber(*ievent, 0,2,t2);

t3 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta3 = (int16_T *) mxGetData(t3);
*ta3 = (int16_T ) el_ievent.eye;
mxSetFieldByNumber(*ievent, 0,3,t3);

t4 = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL);
ta4 = (uint32_T *) mxGetData(t4);
*ta4 = (uint32_T ) el_ievent.sttime;
mxSetFieldByNumber(*ievent, 0,4,t4);

t5 = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL);
ta5 = (uint32_T *) mxGetData(t5);
*ta5 = (uint32_T ) el_ievent.entime;
mxSetFieldByNumber(*ievent, 0,5,t5);

t6 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta6 = (int16_T *) mxGetData(t6);
*ta6 = (int16_T ) el_ievent.hstx;
mxSetFieldByNumber(*ievent, 0,6,t6);

t7 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta7 = (int16_T *) mxGetData(t7);
*ta7 = (int16_T ) el_ievent.hsty;
mxSetFieldByNumber(*ievent, 0,7,t7);

t8 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta8 = (int16_T *) mxGetData(t8);
*ta8 = (int16_T ) el_ievent.gstx;
mxSetFieldByNumber(*ievent, 0,8,t8);

t9 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta9 = (int16_T *) mxGetData(t9);
*ta9 = (int16_T ) el_ievent.gsty;
mxSetFieldByNumber(*ievent, 0,9,t9);

t10 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta10 = (uint16_T *) mxGetData(t10);
*ta10 = (uint16_T ) el_ievent.sta;
mxSetFieldByNumber(*ievent, 0,10,t10);

t11 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta11 = (int16_T *) mxGetData(t11);
*ta11 = (int16_T ) el_ievent.henx;
mxSetFieldByNumber(*ievent, 0,11,t11);

t12 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta12 = (int16_T *) mxGetData(t12);
*ta12 = (int16_T ) el_ievent.heny;
mxSetFieldByNumber(*ievent, 0,12,t12);

t13 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta13 = (int16_T *) mxGetData(t13);
*ta13 = (int16_T ) el_ievent.genx;
mxSetFieldByNumber(*ievent, 0,13,t13);

t14 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta14 = (int16_T *) mxGetData(t14);
*ta14 = (int16_T ) el_ievent.geny;
mxSetFieldByNumber(*ievent, 0,14,t14);

t15 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta15 = (uint16_T *) mxGetData(t15);
*ta15 = (uint16_T ) el_ievent.ena;
mxSetFieldByNumber(*ievent, 0,15,t15);

t16 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta16 = (int16_T *) mxGetData(t16);
*ta16 = (int16_T ) el_ievent.havx;
mxSetFieldByNumber(*ievent, 0,16,t16);

t17 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta17 = (int16_T *) mxGetData(t17);
*ta17 = (int16_T ) el_ievent.havy;
mxSetFieldByNumber(*ievent, 0,17,t17);

t18 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta18 = (int16_T *) mxGetData(t18);
*ta18 = (int16_T ) el_ievent.gavx;
mxSetFieldByNumber(*ievent, 0,18,t18);

t19 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta19 = (int16_T *) mxGetData(t19);
*ta19 = (int16_T ) el_ievent.gavy;
mxSetFieldByNumber(*ievent, 0,19,t19);

t20 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta20 = (uint16_T *) mxGetData(t20);
*ta20 = (uint16_T ) el_ievent.ava;
mxSetFieldByNumber(*ievent, 0,20,t20);

t21 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta21 = (int16_T *) mxGetData(t21);
*ta21 = (int16_T ) el_ievent.avel;
mxSetFieldByNumber(*ievent, 0,21,t21);

t22 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta22 = (int16_T *) mxGetData(t22);
*ta22 = (int16_T ) el_ievent.pvel;
mxSetFieldByNumber(*ievent, 0,22,t22);

t23 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta23 = (int16_T *) mxGetData(t23);
*ta23 = (int16_T ) el_ievent.svel;
mxSetFieldByNumber(*ievent, 0,23,t23);

t24 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta24 = (int16_T *) mxGetData(t24);
*ta24 = (int16_T ) el_ievent.evel;
mxSetFieldByNumber(*ievent, 0,24,t24);

t25 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta25 = (int16_T *) mxGetData(t25);
*ta25 = (int16_T ) el_ievent.supd_x;
mxSetFieldByNumber(*ievent, 0,25,t25);

t26 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta26 = (int16_T *) mxGetData(t26);
*ta26 = (int16_T ) el_ievent.eupd_x;
mxSetFieldByNumber(*ievent, 0,26,t26);

t27 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta27 = (int16_T *) mxGetData(t27);
*ta27 = (int16_T ) el_ievent.supd_y;
mxSetFieldByNumber(*ievent, 0,27,t27);

t28 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta28 = (int16_T *) mxGetData(t28);
*ta28 = (int16_T ) el_ievent.eupd_y;
mxSetFieldByNumber(*ievent, 0,28,t28);

t29 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta29 = (uint16_T *) mxGetData(t29);
*ta29 = (uint16_T ) el_ievent.status;
mxSetFieldByNumber(*ievent, 0,29,t29);


return;
}
void fevent2struct( FEVENT el_fevent, mxArray **fevent ){
mxArray *t0;
uint32_T *ta0;
mxArray *t1;
int16_T *ta1;
mxArray *t2;
uint16_T *ta2;
mxArray *t3;
int16_T *ta3;
mxArray *t4;
uint32_T *ta4;
mxArray *t5;
uint32_T *ta5;
mxArray *t6;
float *ta6;
mxArray *t7;
float *ta7;
mxArray *t8;
float *ta8;
mxArray *t9;
float *ta9;
mxArray *t10;
float *ta10;
mxArray *t11;
float *ta11;
mxArray *t12;
float *ta12;
mxArray *t13;
float *ta13;
mxArray *t14;
float *ta14;
mxArray *t15;
float *ta15;
mxArray *t16;
float *ta16;
mxArray *t17;
float *ta17;
mxArray *t18;
float *ta18;
mxArray *t19;
float *ta19;
mxArray *t20;
float *ta20;
mxArray *t21;
float *ta21;
mxArray *t22;
float *ta22;
mxArray *t23;
float *ta23;
mxArray *t24;
float *ta24;
mxArray *t25;
float *ta25;
mxArray *t26;
float *ta26;
mxArray *t27;
float *ta27;
mxArray *t28;
float *ta28;
mxArray *t29;
uint16_T *ta29;

const char *fields[] = {"time","type","read","eye","sttime","entime","hstx","hsty",
"gstx","gsty","sta","henx","heny","genx","geny","ena","havx","havy","gavx",
"gavy","ava", "avel","pvel","svel","evel","supd_x","eupd_x","supd_y",
"eupd_y","status"};


*fevent = mxCreateStructMatrix(1,1,30,fields);

t0 = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL);
ta0 = (uint32_T *) mxGetData(t0);
*ta0 = (uint32_T ) el_fevent.time;
mxSetFieldByNumber(*fevent, 0,0,t0);

t1 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta1 = (int16_T *) mxGetData(t1);
*ta1 = (int16_T ) el_fevent.type;
mxSetFieldByNumber(*fevent, 0,1,t1);

t2 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta2 = (uint16_T *) mxGetData(t2);
*ta2 = (uint16_T ) el_fevent.read;
mxSetFieldByNumber(*fevent, 0,2,t2);

t3 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta3 = (int16_T *) mxGetData(t3);
*ta3 = (int16_T ) el_fevent.eye;
mxSetFieldByNumber(*fevent, 0,3,t3);

t4 = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL);
ta4 = (uint32_T *) mxGetData(t4);
*ta4 = (uint32_T ) el_fevent.sttime;
mxSetFieldByNumber(*fevent, 0,4,t4);

t5 = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL);
ta5 = (uint32_T *) mxGetData(t5);
*ta5 = (uint32_T ) el_fevent.entime;
mxSetFieldByNumber(*fevent, 0,5,t5);

t6 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta6 = (float *) mxGetData(t6);
*ta6 = (float ) el_fevent.hstx;
mxSetFieldByNumber(*fevent, 0,6,t6);

t7 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta7 = (float *) mxGetData(t7);
*ta7 = (float ) el_fevent.hsty;
mxSetFieldByNumber(*fevent, 0,7,t7);

t8 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta8 = (float *) mxGetData(t8);
*ta8 = (float ) el_fevent.gstx;
mxSetFieldByNumber(*fevent, 0,8,t8);

t9 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta9 = (float *) mxGetData(t9);
*ta9 = (float ) el_fevent.gsty;
mxSetFieldByNumber(*fevent, 0,9,t9);

t10 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta10 = (float *) mxGetData(t10);
*ta10 = (float ) el_fevent.sta;
mxSetFieldByNumber(*fevent, 0,10,t10);

t11 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta11 = (float *) mxGetData(t11);
*ta11 = (float ) el_fevent.henx;
mxSetFieldByNumber(*fevent, 0,11,t11);

t12 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta12 = (float *) mxGetData(t12);
*ta12 = (float ) el_fevent.heny;
mxSetFieldByNumber(*fevent, 0,12,t12);

t13 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta13 = (float *) mxGetData(t13);
*ta13 = (float ) el_fevent.genx;
mxSetFieldByNumber(*fevent, 0,13,t13);

t14 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta14 = (float *) mxGetData(t14);
*ta14 = (float ) el_fevent.geny;
mxSetFieldByNumber(*fevent, 0,14,t14);

t15 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta15 = (float *) mxGetData(t15);
*ta15 = (float ) el_fevent.ena;
mxSetFieldByNumber(*fevent, 0,15,t15);

t16 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta16 = (float *) mxGetData(t16);
*ta16 = (float ) el_fevent.havx;
mxSetFieldByNumber(*fevent, 0,16,t16);

t17 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta17 = (float *) mxGetData(t17);
*ta17 = (float ) el_fevent.havy;
mxSetFieldByNumber(*fevent, 0,17,t17);

t18 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta18 = (float *) mxGetData(t18);
*ta18 = (float ) el_fevent.gavx;
mxSetFieldByNumber(*fevent, 0,18,t18);

t19 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta19 = (float *) mxGetData(t19);
*ta19 = (float ) el_fevent.gavy;
mxSetFieldByNumber(*fevent, 0,19,t19);

t20 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta20 = (float *) mxGetData(t20);
*ta20 = (float ) el_fevent.ava;
mxSetFieldByNumber(*fevent, 0,20,t20);

t21 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta21 = (float *) mxGetData(t21);
*ta21 = (float ) el_fevent.avel;
mxSetFieldByNumber(*fevent, 0,21,t21);

t22 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta22 = (float *) mxGetData(t22);
*ta22 = (float ) el_fevent.pvel;
mxSetFieldByNumber(*fevent, 0,22,t22);

t23 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta23 = (float *) mxGetData(t23);
*ta23 = (float ) el_fevent.svel;
mxSetFieldByNumber(*fevent, 0,23,t23);

t24 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta24 = (float *) mxGetData(t24);
*ta24 = (float ) el_fevent.evel;
mxSetFieldByNumber(*fevent, 0,24,t24);

t25 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta25 = (float *) mxGetData(t25);
*ta25 = (float ) el_fevent.supd_x;
mxSetFieldByNumber(*fevent, 0,25,t25);

t26 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta26 = (float *) mxGetData(t26);
*ta26 = (float ) el_fevent.eupd_x;
mxSetFieldByNumber(*fevent, 0,26,t26);

t27 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta27 = (float *) mxGetData(t27);
*ta27 = (float ) el_fevent.supd_y;
mxSetFieldByNumber(*fevent, 0,27,t27);

t28 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta28 = (float *) mxGetData(t28);
*ta28 = (float ) el_fevent.eupd_y;
mxSetFieldByNumber(*fevent, 0,28,t28);

t29 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta29 = (uint16_T *) mxGetData(t29);
*ta29 = (uint16_T ) el_fevent.status;
mxSetFieldByNumber(*fevent, 0,29,t29);


return;
}
void isample2struct( ISAMPLE el_isample, mxArray **isample ){
const char *fieldnames[] = {"time","type","flags","px","py","hx","hy",
    "pa","gx","gy","rx","ry","status","input","buttons","htype","hdata"};
mxArray *t0;
uint32_T *ta0;
mxArray *t1;
int16_T *ta1;
mxArray *t2;
uint16_T *ta2;
mxArray *t3;
int16_T *ta3;
mxArray *t4;
int16_T *ta4;
mxArray *t5;
int16_T *ta5;
mxArray *t6;
int16_T *ta6;
mxArray *t7;
uint16_T *ta7;
mxArray *t8;
int16_T *ta8;
mxArray *t9;
int16_T *ta9;
mxArray *t10;
int16_T *ta10;
mxArray *t11;
int16_T *ta11;
mxArray *t12;
uint16_T *ta12;
mxArray *t13;
uint16_T *ta13;
mxArray *t14;
uint16_T *ta14;
mxArray *t15;
int16_T *ta15;
mxArray *t16;
int16_T *ta16;

*isample = mxCreateStructMatrix(1,1,17,fieldnames);

t0 = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL);
ta0 = (uint32_T *) mxGetData(t0);
*ta0 = (uint32_T ) el_isample.time;
mxSetFieldByNumber(*isample, 0,0,t0);

t1 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta1 = (int16_T *) mxGetData(t1);
*ta1 = (int16_T ) el_isample.type;
mxSetFieldByNumber(*isample, 0,1,t1);

t2 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta2 = (uint16_T *) mxGetData(t2);
*ta2 = (uint16_T ) el_isample.flags;
mxSetFieldByNumber(*isample, 0,2,t2);

t3 = mxCreateNumericMatrix(1,2,mxINT16_CLASS,mxREAL);
ta3 = (int16_T *) mxGetData(t3);
ta3[0] = (int16_T ) el_isample.px[0];
ta3[1] = (int16_T ) el_isample.px[1];
mxSetFieldByNumber(*isample, 0,3,t3);

t4 = mxCreateNumericMatrix(1,2,mxINT16_CLASS,mxREAL);
ta4 = (int16_T *) mxGetData(t4);
ta4[0] = (int16_T ) el_isample.py[0];
ta4[1] = (int16_T ) el_isample.py[1];
mxSetFieldByNumber(*isample, 0,4,t4);

t5 = mxCreateNumericMatrix(1,2,mxINT16_CLASS,mxREAL);
ta5 = (int16_T *) mxGetData(t5);
ta5[0] = (int16_T ) el_isample.hx[0];
ta5[1] = (int16_T ) el_isample.hx[1];
mxSetFieldByNumber(*isample, 0,5,t5);

t6 = mxCreateNumericMatrix(1,2,mxINT16_CLASS,mxREAL);
ta6 = (int16_T *) mxGetData(t6);
ta6[0] = (int16_T ) el_isample.hy[0];
ta6[1] = (int16_T ) el_isample.hy[1];
mxSetFieldByNumber(*isample, 0,6,t6);

t7 = mxCreateNumericMatrix(1,2,mxUINT16_CLASS,mxREAL);
ta7 = (uint16_T *) mxGetData(t7);
ta7[0] = (uint16_T ) el_isample.pa[0];
ta7[1] = (uint16_T ) el_isample.pa[1];
mxSetFieldByNumber(*isample, 0,7,t7);

t8 = mxCreateNumericMatrix(1,2,mxINT16_CLASS,mxREAL);
ta8 = (int16_T *) mxGetData(t8);
ta8[0] = (int16_T ) el_isample.gx[0];
ta8[1] = (int16_T ) el_isample.gx[1];
mxSetFieldByNumber(*isample, 0,8,t8);

t9 = mxCreateNumericMatrix(1,2,mxINT16_CLASS,mxREAL);
ta9 = (int16_T *) mxGetData(t9);
ta9[0] = (int16_T ) el_isample.gy[0];
ta9[1] = (int16_T ) el_isample.gy[1];
mxSetFieldByNumber(*isample, 0,9,t9);

t10 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta10 = (int16_T *) mxGetData(t10);
*ta10 = (int16_T ) el_isample.rx;
mxSetFieldByNumber(*isample, 0,10,t10);

t11 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta11 = (int16_T *) mxGetData(t11);
*ta11 = (int16_T ) el_isample.ry;
mxSetFieldByNumber(*isample, 0,11,t11);

t12 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta12 = (uint16_T *) mxGetData(t12);
*ta12 = (uint16_T ) el_isample.status;
mxSetFieldByNumber(*isample, 0,12,t12);

t13 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta13 = (uint16_T *) mxGetData(t13);
*ta13 = (uint16_T ) el_isample.input;
mxSetFieldByNumber(*isample, 0,13,t13);

t14 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta14 = (uint16_T *) mxGetData(t14);
*ta14 = (uint16_T ) el_isample.buttons;
mxSetFieldByNumber(*isample, 0,14,t14);

t15 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta15 = (int16_T *) mxGetData(t15);
*ta15 = (int16_T ) el_isample.htype;
mxSetFieldByNumber(*isample, 0,15,t15);

t16 = mxCreateNumericMatrix(1,8,mxINT16_CLASS,mxREAL);
ta16 = (int16_T *) mxGetData(t16);
ta16[0] = (int16_T ) el_isample.hdata[0];
ta16[1] = (int16_T ) el_isample.hdata[1];
ta16[2] = (int16_T ) el_isample.hdata[2];
ta16[3] = (int16_T ) el_isample.hdata[3];
ta16[4] = (int16_T ) el_isample.hdata[4];
ta16[5] = (int16_T ) el_isample.hdata[5];
ta16[6] = (int16_T ) el_isample.hdata[6];
ta16[7] = (int16_T ) el_isample.hdata[7];
mxSetFieldByNumber(*isample, 0,16,t16);

return; 
}

void fsample2struct( FSAMPLE el_fsample, mxArray **fsample ){
const char *fieldnames[] = {"time","type","flags","px","py","hx","hy",
    "pa","gx","gy","rx","ry","status","input","buttons","htype","hdata"};
mxArray *t0;
uint32_T *ta0;
mxArray *t1;
int16_T *ta1;
mxArray *t2;
uint16_T *ta2;
mxArray *t3;
float *ta3;
mxArray *t4;
float *ta4;
mxArray *t5;
float *ta5;
mxArray *t6;
float *ta6;
mxArray *t7;
float *ta7;
mxArray *t8;
float *ta8;
mxArray *t9;
float *ta9;
mxArray *t10;
float *ta10;
mxArray *t11;
float *ta11;
mxArray *t12;
uint16_T *ta12;
mxArray *t13;
uint16_T *ta13;
mxArray *t14;
uint16_T *ta14;
mxArray *t15;
int16_T *ta15;
mxArray *t16;
int16_T *ta16;

*fsample = mxCreateStructMatrix(1,1,17,fieldnames);

t0 = mxCreateNumericMatrix(1,1,mxUINT32_CLASS,mxREAL);
ta0 = (uint32_T *) mxGetData(t0);
*ta0 = (uint32_T ) el_fsample.time;
mxSetFieldByNumber(*fsample, 0,0,t0);

t1 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta1 = (int16_T *) mxGetData(t1);
*ta1 = (int16_T ) el_fsample.type;
mxSetFieldByNumber(*fsample, 0,1,t1);

t2 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta2 = (uint16_T *) mxGetData(t2);
*ta2 = (uint16_T ) el_fsample.flags;
mxSetFieldByNumber(*fsample, 0,2,t2);

t3 = mxCreateNumericMatrix(1,2,mxSINGLE_CLASS,mxREAL);
ta3 = (float *) mxGetData(t3);
ta3[0] = (float ) el_fsample.px[0];
ta3[1] = (float ) el_fsample.px[1];
mxSetFieldByNumber(*fsample, 0,3,t3);

t4 = mxCreateNumericMatrix(1,2,mxSINGLE_CLASS,mxREAL);
ta4 = (float *) mxGetData(t4);
ta4[0] = (float ) el_fsample.py[0];
ta4[1] = (float ) el_fsample.py[1];
mxSetFieldByNumber(*fsample, 0,4,t4);

t5 = mxCreateNumericMatrix(1,2,mxSINGLE_CLASS,mxREAL);
ta5 = (float *) mxGetData(t5);
ta5[0] = (float ) el_fsample.hx[0];
ta5[1] = (float ) el_fsample.hx[1];
mxSetFieldByNumber(*fsample, 0,5,t5);

t6 = mxCreateNumericMatrix(1,2,mxSINGLE_CLASS,mxREAL);
ta6 = (float *) mxGetData(t6);
ta6[0] = (float ) el_fsample.hy[0];
ta6[1] = (float ) el_fsample.hy[1];
mxSetFieldByNumber(*fsample, 0,6,t6);

t7 = mxCreateNumericMatrix(1,2,mxSINGLE_CLASS,mxREAL);
ta7 = (float *) mxGetData(t7);
ta7[0] = (float ) el_fsample.pa[0];
ta7[1] = (float ) el_fsample.pa[1];
mxSetFieldByNumber(*fsample, 0,7,t7);

t8 = mxCreateNumericMatrix(1,2,mxSINGLE_CLASS,mxREAL);
ta8 = (float *) mxGetData(t8);
ta8[0] = (float ) el_fsample.gx[0];
ta8[1] = (float ) el_fsample.gx[1];
mxSetFieldByNumber(*fsample, 0,8,t8);

t9 = mxCreateNumericMatrix(1,2,mxSINGLE_CLASS,mxREAL);
ta9 = (float *) mxGetData(t9);
ta9[0] = (float ) el_fsample.gy[0];
ta9[1] = (float ) el_fsample.gy[1];
mxSetFieldByNumber(*fsample, 0,9,t9);

t10 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta10 = (float *) mxGetData(t10);
*ta10 = (float ) el_fsample.rx;
mxSetFieldByNumber(*fsample, 0,10,t10);

t11 = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);
ta11 = (float *) mxGetData(t11);
*ta11 = (float ) el_fsample.ry;
mxSetFieldByNumber(*fsample, 0,11,t11);

t12 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta12 = (uint16_T *) mxGetData(t12);
*ta12 = (uint16_T ) el_fsample.status;
mxSetFieldByNumber(*fsample, 0,12,t12);

t13 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta13 = (uint16_T *) mxGetData(t13);
*ta13 = (uint16_T ) el_fsample.input;
mxSetFieldByNumber(*fsample, 0,13,t13);

t14 = mxCreateNumericMatrix(1,1,mxUINT16_CLASS,mxREAL);
ta14 = (uint16_T *) mxGetData(t14);
*ta14 = (uint16_T ) el_fsample.buttons;
mxSetFieldByNumber(*fsample, 0,14,t14);

t15 = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL);
ta15 = (int16_T *) mxGetData(t15);
*ta15 = (int16_T ) el_fsample.htype;
mxSetFieldByNumber(*fsample, 0,15,t15);

t16 = mxCreateNumericMatrix(1,8,mxINT16_CLASS,mxREAL);
ta16 = (int16_T *) mxGetData(t16);
ta16[0] = (int16_T ) el_fsample.hdata[0];
ta16[1] = (int16_T ) el_fsample.hdata[1];
ta16[2] = (int16_T ) el_fsample.hdata[2];
ta16[3] = (int16_T ) el_fsample.hdata[3];
ta16[4] = (int16_T ) el_fsample.hdata[4];
ta16[5] = (int16_T ) el_fsample.hdata[5];
ta16[6] = (int16_T ) el_fsample.hdata[6];
ta16[7] = (int16_T ) el_fsample.hdata[7];
mxSetFieldByNumber(*fsample, 0,16,t16);

return; 
}

